using DA.AnimGraph;
using OdinSerializer;
using System.Collections.Generic;
using System.Linq;

namespace DA.AnimGraph.Editor {

    internal class AGANode_Select : AGANode_BlendBase {

        public override AGANodeModelBase modelProperty { set => model = (AGANodeModel_Select)value; get => model; }
        [OdinSerialize] AGANodeModel_Select model;

        public AGANode_Select(AGANodeModelBase model) : base(model) {
            inputChildren.Add(AUIDGenerator.Gen());
            inputNames.Add("port 0");
            inputChildren.Add(AUIDGenerator.Gen());
            inputNames.Add("port 1");
        }

        public override int Publish(List<PublishNode> outNodes, AGAPublisher.PublishContext context) {
            var pNode = new SelectPNode();
            pNode.uuid = uuid;
            pNode.data = model.Publish(context);
            var index = outNodes.Count;
            outNodes.Add(pNode);

            pNode.inputChildren = new int[inputChildren.Count];
            for (int i = 0; i < inputChildren.Count; i++) {
                var childUuid = inputChildren[i];
                var childNode = context.subGraph.FindNextNodeInstance(uuid, childUuid) as AGANode_FunctionBase;
                if (childNode != null) pNode.inputChildren[i] = AGAPublisher.ExportNode(childNode, outNodes, context);
                else pNode.inputChildren[i] = -1;
            }

            PublishPerks(pNode, context);
            return index;
        }
    }

    [ModelOf(typeof(AGANode_Select))]
    internal class AGANodeModel_Select : AGANodeModelBase {

        public AGAFieldModel_IntLink select = new();
        public float defaultTransitionDuration;
        public List<AGAFieldModel_Transition> transitions = new();
        public AGAFieldModel_SelectInputMapping inputMapping = new();

        public SelectNode.SetupData Publish(AGAPublisher.PublishContext context) {
            return new SelectNode.SetupData() {
                stateDesc = select.Publish(context),
                defaultTransitionDuration = defaultTransitionDuration,
                transitionsDesc = transitions.Select(t => t.Publish()).ToArray(),
                inputMappingDesc = inputMapping.Publish(context),
            };
        }
    }


}
